
import { ElMessage } from "element-plus";
// 把路径转换成驼峰命名
export const pathToCamel = (path) => {
	return path.replace(/\/(\w)/g, (all, letter) => letter.toUpperCase())
}
/**
  * 将时间转换成yyyy-MM-dd格式的字符串
  * @param date
  */
export const dateToString = (date) => {
	if (!date) {
		return "";
	} else {
		if (typeof date === "string" && date.constructor === String) {
			return date;
		} else {
			var y = date.getFullYear();
			var m = date.getMonth() + 1;
			m = m < 10 ? "0" + m : m;
			var d = date.getDate();
			d = d < 10 ? "0" + d : d;
			return y + "-" + m + "-" + d;
		}
	}
}
/**
 * 将time格式字符串截取成Year：Month:day  HH:MM:ss
 * @param str 时间字符串
 */
export const stringToDate = (str) => {
	//截掉尾部+8：00
	let temp = str.replace("+08:00", "");
	temp = temp.replace("T", " ");
	return temp;
}
/**
 * 将时间转换成带时分秒 格式的字符串
 * @param now 时间戳
 */
export const dateToStringBySecond = (now) => {
	if (!now) {
		return "";
	} else {
		if (typeof now === "string" && now.constructor === String) {
			return now;
		} else {
			var year = now.getFullYear();
			var month = (now.getMonth() + 1).toString();
			var day = now.getDate().toString();
			var hour = now.getHours().toString();
			var minute = now.getMinutes().toString();
			var second = now.getSeconds().toString();
			if (month.length == 1) {
				month = "0" + month;
			}
			if (day.length == 1) {
				day = "0" + day;
			}
			if (hour.length == 1) {
				hour = "0" + hour;
			}
			if (minute.length == 1) {
				minute = "0" + minute;
			}
			if (second.length == 1) {
				second = "0" + second;
			}
			var dateTime =
				year +
				"-" +
				month +
				"-" +
				day +
				" " +
				hour +
				":" +
				minute +
				":" +
				second;
			return dateTime;
		}
	}
}

/**
 * 保存文本到文件
 * @param fileName 文件名称
 * @param content 文件内容
 */
export const saveFile = (fileName, content) => {
	let url = window.URL.createObjectURL(new Blob([content]));
	let link = document.createElement("a");
	link.style.display = "none";
	link.href = url;
	link.target = "_blank";
	link.download = fileName;
	document.body.appendChild(link);
	link.click();
	document.body.removeChild(link);
	window.URL.revokeObjectURL(url);
}

/**
 * 复制内容到剪贴板
 * @param content 复制内容
 */
export const copyToClip = (content) => {
	let aux = document.createElement("textarea");
	aux.value = content;
	document.body.appendChild(aux);
	aux.select();
	document.execCommand("copy");
	document.body.removeChild(aux);
	ElMessage({
		type: "success",
		message: "已复制到剪切板",
	});
}
/**
 * 判断是否是字符串
 * @param str 判断内容
 */
export const typeSting = (str) => {
	if (typeof str == "string") {
		try {
			let obj = JSON.parse(str);
			// 等于这个条件说明就是JSON字符串 会返回true
			if (typeof obj == "object" && obj) {
				return true;
			} else {
				//不是就返回false
				return false;
			}
		} catch (e) {
			return false;
		}
	}
	return false;
};
/**
 * 递归解析json
 * @param newObj 解析后的的对象
 * @param oldObj 需要解析的对象
 */
export const deepJSON = (newObj, oldObj) => {
	for (const key in oldObj) {
		if (typeSting(oldObj[key])) {
			let item = JSON.parse(oldObj[key]);
			if (typeof item == "array") {
				newObj[key] = [];
				deepJSON(newObj[key], item);
			} else if (typeof item == "object") {
				newObj[key] = {};
				deepJSON(newObj[key], item);
			}
		} else {
			let item = oldObj[key];
			if (typeof item == "array") {
				newObj[key] = [];
				deepJSON(newObj[key], item);
			} else if (typeof item == "object") {
				newObj[key] = {};
				deepJSON(newObj[key], item);
			} else {
				newObj[key] = item;
			}
		}
	}
	return newObj;
};